﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Deployment.Application;
using System.Windows;

namespace ClimateControl.Controls
{
    public static class UpdateManager
    {
        public static void CheckForUpdate()
        {
            UpdateCheckInfo info = null;

            if (!ApplicationDeployment.IsNetworkDeployed)
                return;

            ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

            try
            {
                info = ad.CheckForDetailedUpdate();
            }
            catch (Exception)
            {
                return;
            }

            if (!info.UpdateAvailable)
                return;

            MessageBox.Show("There's an update! It will now install and restart the application.", "Climate Control Update", MessageBoxButton.OK, MessageBoxImage.Information);
            try
            {
                ad.Update();
                System.Windows.Forms.Application.Restart();
                System.Windows.Application.Current.Shutdown();
            }
            catch (Exception)
            {
                MessageBox.Show("Something went wrong during the upgrade :(");
            }
        }
    }
}
